package cn.edu.nuc.crm.controller;


import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.crm.entity.Role;
import cn.edu.nuc.crm.service.interfaces.RightService;
import cn.edu.nuc.crm.service.interfaces.RoleService;
import cn.edu.nuc.crm.service.interfaces.SysfunctionService;
//import cn.edu.nuc.crm.entity.Right;
import cn.edu.nuc.crm.entity.Sysfunction;

@Controller
public class RoleController {
	@Autowired	
	private RoleService roleService;
	@Autowired
	private SysfunctionService sysService;
	@Autowired	
	private RightService rightService;
	/**
	 * 列出角色列表
	 * @param current
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/rolelist",method=RequestMethod.GET)
	public String login(HttpServletRequest request){
		List<Role> role= roleService.rolelist();
		request.setAttribute("list", role);
		return "role/rolelist";
	}
	
	@RequestMapping(value="/right",method=RequestMethod.GET)
	public String right(int roleid,HttpServletRequest request){
		List<Sysfunction> list=sysService.select1(roleid);
		request.setAttribute("list", list);
		Role role=roleService.choose(roleid);
		request.setAttribute("role", role);
		return "role/right";
	}
	/**
	 * 保存权限
	 * @param roleid
	 * @param ckrr
	 * @param request
	 * @return
	 * @throws SQLException 
	 */
	@RequestMapping(value="/saveright",method=RequestMethod.POST)
	public String save(int roleid,int[] ckrr,HttpServletRequest request) throws SQLException{
		rightService.add(roleid,ckrr);
		List<Role> role= roleService.select();
		request.setAttribute("list", role);
		return "role/rolelist";	
	}
	
	/**
	 * 添加角色功能
	 * @return
	 */
	@RequestMapping(value="role/add",method=RequestMethod.GET)
	public	 String add(){
		return "role/add";		
	}
	@RequestMapping(value="roletoadd",method=RequestMethod.POST)
	public String add(Role role,HttpServletRequest request){
		try{
		roleService.insert(role);
		return "rolelist";
		}catch(Exception e){
		  request.setAttribute("msg", "添加失败");
		  return "function/add";
		}
	}
	@RequestMapping(value="role/rolelist",method=RequestMethod.GET)
	 public String list(){
		return "/role/rolelist";
	}
}